{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from pandas import Series, DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>country</th>\n",
       "      <th>description</th>\n",
       "      <th>points</th>\n",
       "      <th>province</th>\n",
       "      <th>variety</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>US</td>\n",
       "      <td>This tremendous 100% varietal wine hails from ...</td>\n",
       "      <td>96</td>\n",
       "      <td>California</td>\n",
       "      <td>Cabernet Sauvignon</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Spain</td>\n",
       "      <td>Ripe aromas of fig, blackberry and cassis are ...</td>\n",
       "      <td>96</td>\n",
       "      <td>Northern Spain</td>\n",
       "      <td>Tinta de Toro</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>US</td>\n",
       "      <td>Mac Watson honors the memory of a wine once ma...</td>\n",
       "      <td>96</td>\n",
       "      <td>California</td>\n",
       "      <td>Sauvignon Blanc</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>US</td>\n",
       "      <td>This spent 20 months in 30% new French oak, an...</td>\n",
       "      <td>96</td>\n",
       "      <td>Oregon</td>\n",
       "      <td>Pinot Noir</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>France</td>\n",
       "      <td>This is the top wine from La Bégude, named aft...</td>\n",
       "      <td>95</td>\n",
       "      <td>Provence</td>\n",
       "      <td>Provence red blend</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  country                                        description  points  \\\n",
       "0      US  This tremendous 100% varietal wine hails from ...      96   \n",
       "1   Spain  Ripe aromas of fig, blackberry and cassis are ...      96   \n",
       "2      US  Mac Watson honors the memory of a wine once ma...      96   \n",
       "3      US  This spent 20 months in 30% new French oak, an...      96   \n",
       "4  France  This is the top wine from La Bégude, named aft...      95   \n",
       "\n",
       "         province             variety  \n",
       "0      California  Cabernet Sauvignon  \n",
       "1  Northern Spain       Tinta de Toro  \n",
       "2      California     Sauvignon Blanc  \n",
       "3          Oregon          Pinot Noir  \n",
       "4        Provence  Provence red blend  "
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Open the file `winemag-150k-reviews.csv`, and read it into a data frame\n",
    "filename = '../data/winemag-150k-reviews.csv'\n",
    "\n",
    "df = pd.read_csv(filename,\n",
    "                usecols=['country','points','province','description', 'variety'])\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 1\n",
    "\n",
    "Which country's wines got the highest average score for all wines?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "country\n",
       "England     92.888889\n",
       "Austria     89.276742\n",
       "France      88.925870\n",
       "Germany     88.626427\n",
       "Italy       88.413664\n",
       "Canada      88.239796\n",
       "Slovenia    88.234043\n",
       "Morocco     88.166667\n",
       "Turkey      88.096154\n",
       "Portugal    88.057685\n",
       "Name: points, dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    df\n",
    "    .groupby('country')['points'].mean()\n",
    "    .sort_values(ascending=False)\n",
    "    .head(10)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 2\n",
    "\n",
    "Create a pivot table in which the index contains countries, the columns contain varieties, and the cells contain mean scores. Only include the top 10 varieties."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>variety</th>\n",
       "      <th>Bordeaux-style Red Blend</th>\n",
       "      <th>Cabernet Sauvignon</th>\n",
       "      <th>Chardonnay</th>\n",
       "      <th>Merlot</th>\n",
       "      <th>Pinot Noir</th>\n",
       "      <th>Red Blend</th>\n",
       "      <th>Riesling</th>\n",
       "      <th>Sauvignon Blanc</th>\n",
       "      <th>Syrah</th>\n",
       "      <th>Zinfandel</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>country</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Argentina</th>\n",
       "      <td>89.575472</td>\n",
       "      <td>85.527745</td>\n",
       "      <td>84.177489</td>\n",
       "      <td>84.341969</td>\n",
       "      <td>85.058333</td>\n",
       "      <td>88.197059</td>\n",
       "      <td>85.000000</td>\n",
       "      <td>83.295302</td>\n",
       "      <td>85.232394</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Australia</th>\n",
       "      <td>88.841463</td>\n",
       "      <td>88.115502</td>\n",
       "      <td>86.727952</td>\n",
       "      <td>85.258824</td>\n",
       "      <td>86.405263</td>\n",
       "      <td>87.816176</td>\n",
       "      <td>87.790210</td>\n",
       "      <td>86.624060</td>\n",
       "      <td>91.952381</td>\n",
       "      <td>88.200000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Austria</th>\n",
       "      <td>91.625000</td>\n",
       "      <td>87.750000</td>\n",
       "      <td>90.016393</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>88.753846</td>\n",
       "      <td>88.890511</td>\n",
       "      <td>90.583955</td>\n",
       "      <td>88.694215</td>\n",
       "      <td>87.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Brazil</th>\n",
       "      <td>86.000000</td>\n",
       "      <td>81.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>83.200000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>84.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bulgaria</th>\n",
       "      <td>NaN</td>\n",
       "      <td>84.812500</td>\n",
       "      <td>86.875000</td>\n",
       "      <td>84.363636</td>\n",
       "      <td>87.400000</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>83.750000</td>\n",
       "      <td>84.400000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Canada</th>\n",
       "      <td>89.000000</td>\n",
       "      <td>88.666667</td>\n",
       "      <td>88.653846</td>\n",
       "      <td>87.875000</td>\n",
       "      <td>89.111111</td>\n",
       "      <td>89.500000</td>\n",
       "      <td>87.564516</td>\n",
       "      <td>87.750000</td>\n",
       "      <td>89.666667</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Chile</th>\n",
       "      <td>89.754717</td>\n",
       "      <td>86.561564</td>\n",
       "      <td>85.246011</td>\n",
       "      <td>84.939189</td>\n",
       "      <td>85.827273</td>\n",
       "      <td>88.683168</td>\n",
       "      <td>85.714286</td>\n",
       "      <td>85.895805</td>\n",
       "      <td>87.506739</td>\n",
       "      <td>85.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>China</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Croatia</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>85.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>84.000000</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cyprus</th>\n",
       "      <td>NaN</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>85.714286</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Czech Republic</th>\n",
       "      <td>NaN</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>England</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>94.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>France</th>\n",
       "      <td>89.557460</td>\n",
       "      <td>84.080000</td>\n",
       "      <td>89.315353</td>\n",
       "      <td>84.971698</td>\n",
       "      <td>89.430777</td>\n",
       "      <td>87.540984</td>\n",
       "      <td>89.063872</td>\n",
       "      <td>88.026882</td>\n",
       "      <td>89.772926</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Germany</th>\n",
       "      <td>NaN</td>\n",
       "      <td>87.000000</td>\n",
       "      <td>89.166667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>87.075472</td>\n",
       "      <td>87.285714</td>\n",
       "      <td>88.918816</td>\n",
       "      <td>83.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Greece</th>\n",
       "      <td>86.600000</td>\n",
       "      <td>85.837838</td>\n",
       "      <td>85.638889</td>\n",
       "      <td>86.142857</td>\n",
       "      <td>NaN</td>\n",
       "      <td>86.050847</td>\n",
       "      <td>NaN</td>\n",
       "      <td>86.526316</td>\n",
       "      <td>86.733333</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hungary</th>\n",
       "      <td>85.818182</td>\n",
       "      <td>86.666667</td>\n",
       "      <td>83.000000</td>\n",
       "      <td>88.200000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>84.454545</td>\n",
       "      <td>NaN</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>India</th>\n",
       "      <td>NaN</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>89.500000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Israel</th>\n",
       "      <td>88.615385</td>\n",
       "      <td>87.753521</td>\n",
       "      <td>86.746479</td>\n",
       "      <td>86.078125</td>\n",
       "      <td>85.615385</td>\n",
       "      <td>87.760563</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>84.666667</td>\n",
       "      <td>87.481481</td>\n",
       "      <td>85.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Italy</th>\n",
       "      <td>88.600000</td>\n",
       "      <td>89.536680</td>\n",
       "      <td>88.367906</td>\n",
       "      <td>89.864952</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>89.042238</td>\n",
       "      <td>87.500000</td>\n",
       "      <td>86.517241</td>\n",
       "      <td>89.246862</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Lebanon</th>\n",
       "      <td>NaN</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>85.529412</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Luxembourg</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>86.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Macedonia</th>\n",
       "      <td>NaN</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>84.000000</td>\n",
       "      <td>82.500000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Mexico</th>\n",
       "      <td>87.500000</td>\n",
       "      <td>83.666667</td>\n",
       "      <td>83.375000</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>87.500000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>84.400000</td>\n",
       "      <td>85.500000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Moldova</th>\n",
       "      <td>90.000000</td>\n",
       "      <td>84.545455</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>85.000000</td>\n",
       "      <td>81.000000</td>\n",
       "      <td>85.400000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>84.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Morocco</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>88.000000</td>\n",
       "      <td>90.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>New Zealand</th>\n",
       "      <td>88.905405</td>\n",
       "      <td>86.944444</td>\n",
       "      <td>87.810573</td>\n",
       "      <td>86.737705</td>\n",
       "      <td>87.731626</td>\n",
       "      <td>89.800000</td>\n",
       "      <td>87.946667</td>\n",
       "      <td>87.287120</td>\n",
       "      <td>87.674419</td>\n",
       "      <td>90.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Portugal</th>\n",
       "      <td>89.875000</td>\n",
       "      <td>88.720000</td>\n",
       "      <td>87.642857</td>\n",
       "      <td>86.142857</td>\n",
       "      <td>88.928571</td>\n",
       "      <td>87.189189</td>\n",
       "      <td>NaN</td>\n",
       "      <td>85.727273</td>\n",
       "      <td>88.482143</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Romania</th>\n",
       "      <td>NaN</td>\n",
       "      <td>85.578947</td>\n",
       "      <td>84.071429</td>\n",
       "      <td>85.263158</td>\n",
       "      <td>85.333333</td>\n",
       "      <td>84.600000</td>\n",
       "      <td>82.857143</td>\n",
       "      <td>85.714286</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Serbia</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>87.500000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Slovakia</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>83.666667</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Slovenia</th>\n",
       "      <td>89.750000</td>\n",
       "      <td>88.500000</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>88.285714</td>\n",
       "      <td>88.833333</td>\n",
       "      <td>86.666667</td>\n",
       "      <td>86.750000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>South Africa</th>\n",
       "      <td>89.766129</td>\n",
       "      <td>88.018433</td>\n",
       "      <td>87.091241</td>\n",
       "      <td>85.825397</td>\n",
       "      <td>88.166667</td>\n",
       "      <td>87.832536</td>\n",
       "      <td>86.400000</td>\n",
       "      <td>86.368421</td>\n",
       "      <td>88.057143</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spain</th>\n",
       "      <td>86.333333</td>\n",
       "      <td>85.689189</td>\n",
       "      <td>84.550725</td>\n",
       "      <td>84.666667</td>\n",
       "      <td>85.757576</td>\n",
       "      <td>87.483968</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>83.568627</td>\n",
       "      <td>87.548387</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Switzerland</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>84.500000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Turkey</th>\n",
       "      <td>86.666667</td>\n",
       "      <td>91.000000</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>88.500000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>87.700000</td>\n",
       "      <td>NaN</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>89.000000</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>US</th>\n",
       "      <td>89.311681</td>\n",
       "      <td>88.555786</td>\n",
       "      <td>87.729543</td>\n",
       "      <td>86.630028</td>\n",
       "      <td>88.877273</td>\n",
       "      <td>87.326671</td>\n",
       "      <td>87.397094</td>\n",
       "      <td>86.784223</td>\n",
       "      <td>88.359850</td>\n",
       "      <td>86.664819</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Uruguay</th>\n",
       "      <td>NaN</td>\n",
       "      <td>83.500000</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>83.500000</td>\n",
       "      <td>82.000000</td>\n",
       "      <td>85.387097</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "variety         Bordeaux-style Red Blend  Cabernet Sauvignon  Chardonnay  \\\n",
       "country                                                                    \n",
       "Argentina                      89.575472           85.527745   84.177489   \n",
       "Australia                      88.841463           88.115502   86.727952   \n",
       "Austria                        91.625000           87.750000   90.016393   \n",
       "Brazil                         86.000000           81.000000         NaN   \n",
       "Bulgaria                             NaN           84.812500   86.875000   \n",
       "Canada                         89.000000           88.666667   88.653846   \n",
       "Chile                          89.754717           86.561564   85.246011   \n",
       "China                                NaN                 NaN   82.000000   \n",
       "Croatia                              NaN                 NaN   85.000000   \n",
       "Cyprus                               NaN           88.000000         NaN   \n",
       "Czech Republic                       NaN           86.000000         NaN   \n",
       "England                              NaN                 NaN   94.000000   \n",
       "France                         89.557460           84.080000   89.315353   \n",
       "Germany                              NaN           87.000000   89.166667   \n",
       "Greece                         86.600000           85.837838   85.638889   \n",
       "Hungary                        85.818182           86.666667   83.000000   \n",
       "India                                NaN           82.000000         NaN   \n",
       "Israel                         88.615385           87.753521   86.746479   \n",
       "Italy                          88.600000           89.536680   88.367906   \n",
       "Lebanon                              NaN           82.000000         NaN   \n",
       "Luxembourg                           NaN                 NaN         NaN   \n",
       "Macedonia                            NaN           82.000000         NaN   \n",
       "Mexico                         87.500000           83.666667   83.375000   \n",
       "Moldova                        90.000000           84.545455   82.000000   \n",
       "Morocco                              NaN                 NaN   88.000000   \n",
       "New Zealand                    88.905405           86.944444   87.810573   \n",
       "Portugal                       89.875000           88.720000   87.642857   \n",
       "Romania                              NaN           85.578947   84.071429   \n",
       "Serbia                               NaN                 NaN         NaN   \n",
       "Slovakia                             NaN                 NaN         NaN   \n",
       "Slovenia                       89.750000           88.500000   89.000000   \n",
       "South Africa                   89.766129           88.018433   87.091241   \n",
       "Spain                          86.333333           85.689189   84.550725   \n",
       "Switzerland                          NaN                 NaN         NaN   \n",
       "Turkey                         86.666667           91.000000   89.000000   \n",
       "US                             89.311681           88.555786   87.729543   \n",
       "Uruguay                              NaN           83.500000   82.000000   \n",
       "\n",
       "variety            Merlot  Pinot Noir  Red Blend   Riesling  Sauvignon Blanc  \\\n",
       "country                                                                        \n",
       "Argentina       84.341969   85.058333  88.197059  85.000000        83.295302   \n",
       "Australia       85.258824   86.405263  87.816176  87.790210        86.624060   \n",
       "Austria         89.000000   88.753846  88.890511  90.583955        88.694215   \n",
       "Brazil          83.200000         NaN  84.000000        NaN              NaN   \n",
       "Bulgaria        84.363636   87.400000  89.000000  83.750000        84.400000   \n",
       "Canada          87.875000   89.111111  89.500000  87.564516        87.750000   \n",
       "Chile           84.939189   85.827273  88.683168  85.714286        85.895805   \n",
       "China                 NaN         NaN        NaN        NaN              NaN   \n",
       "Croatia               NaN   84.000000  86.000000        NaN        82.000000   \n",
       "Cyprus                NaN         NaN  85.714286        NaN              NaN   \n",
       "Czech Republic        NaN         NaN        NaN        NaN              NaN   \n",
       "England               NaN         NaN        NaN        NaN              NaN   \n",
       "France          84.971698   89.430777  87.540984  89.063872        88.026882   \n",
       "Germany               NaN   87.075472  87.285714  88.918816        83.000000   \n",
       "Greece          86.142857         NaN  86.050847        NaN        86.526316   \n",
       "Hungary         88.200000   90.000000  84.454545        NaN        86.000000   \n",
       "India                 NaN         NaN        NaN        NaN        89.500000   \n",
       "Israel          86.078125   85.615385  87.760563  90.000000        84.666667   \n",
       "Italy           89.864952   86.000000  89.042238  87.500000        86.517241   \n",
       "Lebanon               NaN         NaN  85.529412        NaN              NaN   \n",
       "Luxembourg            NaN         NaN        NaN  86.000000              NaN   \n",
       "Macedonia             NaN   84.000000  82.500000        NaN              NaN   \n",
       "Mexico          82.000000         NaN  87.500000        NaN        84.400000   \n",
       "Moldova         85.000000   81.000000  85.400000        NaN        84.000000   \n",
       "Morocco               NaN         NaN  89.000000        NaN        88.000000   \n",
       "New Zealand     86.737705   87.731626  89.800000  87.946667        87.287120   \n",
       "Portugal        86.142857   88.928571  87.189189        NaN        85.727273   \n",
       "Romania         85.263158   85.333333  84.600000  82.857143        85.714286   \n",
       "Serbia                NaN         NaN  89.000000  87.500000              NaN   \n",
       "Slovakia              NaN         NaN        NaN  83.666667              NaN   \n",
       "Slovenia        89.000000   88.285714  88.833333  86.666667        86.750000   \n",
       "South Africa    85.825397   88.166667  87.832536  86.400000        86.368421   \n",
       "Spain           84.666667   85.757576  87.483968  82.000000        83.568627   \n",
       "Switzerland     84.500000         NaN        NaN        NaN              NaN   \n",
       "Turkey          88.500000         NaN  87.700000        NaN        89.000000   \n",
       "US              86.630028   88.877273  87.326671  87.397094        86.784223   \n",
       "Uruguay         83.500000   82.000000  85.387097        NaN              NaN   \n",
       "\n",
       "variety             Syrah  Zinfandel  \n",
       "country                               \n",
       "Argentina       85.232394        NaN  \n",
       "Australia       91.952381  88.200000  \n",
       "Austria         87.000000        NaN  \n",
       "Brazil                NaN        NaN  \n",
       "Bulgaria        90.000000        NaN  \n",
       "Canada          89.666667        NaN  \n",
       "Chile           87.506739  85.000000  \n",
       "China                 NaN        NaN  \n",
       "Croatia               NaN        NaN  \n",
       "Cyprus                NaN        NaN  \n",
       "Czech Republic        NaN        NaN  \n",
       "England               NaN        NaN  \n",
       "France          89.772926        NaN  \n",
       "Germany               NaN        NaN  \n",
       "Greece          86.733333        NaN  \n",
       "Hungary               NaN        NaN  \n",
       "India                 NaN        NaN  \n",
       "Israel          87.481481  85.000000  \n",
       "Italy           89.246862        NaN  \n",
       "Lebanon               NaN        NaN  \n",
       "Luxembourg            NaN        NaN  \n",
       "Macedonia             NaN        NaN  \n",
       "Mexico          85.500000        NaN  \n",
       "Moldova               NaN        NaN  \n",
       "Morocco         90.000000        NaN  \n",
       "New Zealand     87.674419  90.000000  \n",
       "Portugal        88.482143        NaN  \n",
       "Romania         82.000000        NaN  \n",
       "Serbia                NaN        NaN  \n",
       "Slovakia              NaN        NaN  \n",
       "Slovenia              NaN        NaN  \n",
       "South Africa    88.057143        NaN  \n",
       "Spain           87.548387        NaN  \n",
       "Switzerland           NaN        NaN  \n",
       "Turkey          89.000000        NaN  \n",
       "US              88.359850  86.664819  \n",
       "Uruguay               NaN        NaN  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    df.loc[df['variety']\n",
    "           .isin(df['variety']\n",
    "                 .value_counts()\n",
    "                 .head(10)\n",
    "                 .index)]\n",
    "            .pivot_table(index='country', \n",
    "                         columns='variety', \n",
    "                         values='points')\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Beyond 3\n",
    "\n",
    "What is the correlation between the number of wines offered by a country, and the mean score for that country? That is: If a country enters more wines, does its average score in reviews go up?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.236117</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>0.236117</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          count      mean\n",
       "count  1.000000  0.236117\n",
       "mean   0.236117  1.000000"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(\n",
    "    df\n",
    "    .groupby('country')['points']\n",
    "    .agg(['count', 'mean'])\n",
    "    .corr()\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
